package MainService;

/**
 * Created by ghost461 on 17-6-28.
 */
import PartService.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import DataService.*;
import RedisService.RedisCURD;
import SqlService.*;

public class ThreadForSelect implements Runnable{
    int num = 1;
    private Thread t;
    private String threadName;

    ThreadForSelect( String name) {
        threadName = name;
        System.out.println("Creating " +  threadName );
    }

    public void run() {
        System.out.println("Running " +  threadName );
        //连接JDBC和数据库
        Sql sql = new Sql();
        sql.getCon();
        while(true){
            //构造新的json解析器和json容器
            DataFormer dataformer = new DataFormer();
            JsonBean jsonbean = new JsonBean();
            JsonBean2 jsonbean2 = new JsonBean2();
            //JsonBean3 jsonbean3 = new JsonBean3();
            //测试用的一段json,冒号前面加反斜杠是为了转义
            //String s =  "{\"id\": \"007db07f\", \"data\": [ {\"mac\": \"00:25:9e:27:38:f7\", \"rssi\": \"-66\", \"ch\": \"6\", \"range\": \"108\"}, {\"mac\": \"00:25:9e:27:38:f8\", \"rssi\": \"-66\", \"ch\": \"6\", \"range\": \"108\"}, {\"mac\": \"00:25:9e:27:38:f9\", \"rssi\": \"-67\", \"ch\": \"6\", \"range\": \"118\"}, {\"mac\": \"00:25:9e:07:b6:cb\", \"rssi\": \"-85\", \"ch\": \"6\", \"range\": \"550\"}, {\"mac\": \"00:25:9e:5f:f1:d8\", \"rssi\": \"-75\", \"ch\": \"2\", \"range\": \"234\"}],\"mmac\": \"20:f4:1b:7d:b0:7f\",\"rate\": \"1\", \"time\": \"Wed Mar 22 16:52:41 2017\",\"lat\": \"\",\"lon\": \"\"}";
            RedisCURD Redis = new RedisCURD();
            String information = Redis.select(num);
            System.out.println(information);
            //System.out.println(num);
            while(information == null){
                try {
                    Thread.sleep(1);
                }catch (InterruptedException e){
                    System.out.println("Thread " +  threadName + " interrupted.");
                }
            }
            //json解析
            jsonbean = dataformer.ParseToJavaBean(information, jsonbean);
            JSONArray jsondata = JSON.parseArray(jsonbean.data);
            int i=0;
            com.alibaba.fastjson.JSONObject info;
            CURD curd = new CURD();
            while(i<jsondata.size())
            {
            	info = jsondata.getJSONObject(i);
            	jsonbean2.mac = info.getString("mac");
                jsonbean2.ranges = info.getIntValue("range");
                jsonbean2.rssi = info.getIntValue("rssi");
                SimpleDateFormat sdf = new SimpleDateFormat( "EEE MMM dd HH:mm:ss yyyy",Locale.ENGLISH);
                SimpleDateFormat sdf2 = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
                Date date;
				try {
					date = sdf.parse(jsonbean.time);
					date = sdf2.parse(sdf2.format(date));
					jsonbean2.times=new java.sql.Timestamp(date.getTime());
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                jsonbean2.wifimac=jsonbean.mmac;
                curd.Insert2(jsonbean2);
                System.out.println(i);
                i++;
            }
            
            
            //调试输出
            /*System.out.println(jsonbean.id);
            System.out.println(jsonbean.data);
            System.out.println(jsonbean.mmac);
            System.out.println(jsonbean.rate);
            System.out.println(jsonbean.time);
            System.out.println(jsonbean.lat);
            System.out.println(jsonbean.lon);
            */
            //测试用的sql语句
            //String query = "insert into test.testtab values(\"6666\",\"2333\");";
            //创建数据库操作类，并将数据写入数据库
            
            num++;
        }
    }

    public void start () {
    	int i=0;
        System.out.println("Starting " +  threadName );
        System.out.println(i);
        if (t == null) {
            t = new Thread (this, threadName);
            t.start ();
        }
    }
}

